package com.yehuishou.controller;



import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yehuishou.pojo.BackOrder;
import com.yehuishou.pojo.User;
import com.yehuishou.service.BackOrderService;
import com.yehuishou.service.ReceiverAddressService;
import com.yehuishou.util.KdGoldAPIDemo;

@Controller
public class BackOrderController {
	
	@Autowired
	private ReceiverAddressService receiverAddressService;
	
	@Resource
    private BackOrderService backOrderService;
    @RequestMapping("yuyuequjian1")
    public  String  submitOrder(HttpSession session,HttpServletRequest request,BackOrder backOrder,String phone,String state,String city,String address,String district,
    		String ShipperCode,Integer PayType,Integer ExpType,
			String jName,String jMobile,String jProvinceNam,
			String jCityName,String jExpAreaName,String jAddress,
			String sname,String smobile,String sprovincenam,String scityname,String sexpareaname,
			String saddress,String GoodsName,
			String backprice,String back_valids
			
    		){
    	 
    		String OrderCode;
			SimpleDateFormat fmd = new SimpleDateFormat("yyyyMMddHHssmmSS");
			
			Thread tr =new Thread();
			
					synchronized (tr) {
						OrderCode = fmd.format(new Date());
						System.out.println(OrderCode);
					}
					
				KdGoldAPIDemo yuyuequjian =new KdGoldAPIDemo();
			try {
				state =request.getParameter("jProvinceNam");
		    	 User user = (User) session.getAttribute("user");
		    	    Integer uid=user.getUid();
		    	    
		    	    if (uid!=null) {
		    	    	backOrder.setUid(uid);
						backOrderService.delectAddress(backOrder);
					}
		    	     phone=user.getPhone();
		    	 String payment= request.getParameter("pay");
		    	 city=request.getParameter("jCityName");
		    	 district=request.getParameter("jExpAreaName");
		    	 address=request.getParameter("jAddress");
		    	
		    	  back_valids=request.getParameter("back_valids");
		         Integer back_id=Integer.parseInt(request.getParameter("back_id"));
		         Integer num=1;
		    	 Date date=new Date();
		    	 Long long1=date.getTime();
		    	 SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		    	 String time=formatter.format(date);
		    		 SimpleDateFormat formatter1=new SimpleDateFormat("yyyyMMddHHmmss");
			    	 String time1=formatter1.format(date);
			    	 synchronized (time1) {
			    		 backOrder.setOid(time1);
					}
		    	  String string= String.valueOf(long1);
		    	  
		    	 backOrder.setPayment(payment);
		    	 backOrder.setOrder_type(1);
		    	 backOrder.setPayment_type(1);
		    	 backOrder.setStatus(1);
		    	 
				
				 backOrder.setCreatetime(time);
			    	backOrder.setName(request.getParameter("jName"));
			    	backOrder.setUid(uid);
			    	backOrder.setState(state);
			    	backOrder.setCity(city);
			    	backOrder.setAddress(address);
			    	backOrder.setDistrict(district);
			    	backOrder.setBack_id(back_id);
			    	backOrder.setNum(num);
			    	backOrder.setBack_way(Integer.parseInt(request.getParameter("back_way_three")));
			    	backOrder.setBack_assess(back_valids);
			    	String result =yuyuequjian.orderOnlineByJson(OrderCode,ShipperCode,PayType,ExpType,
							jName,jMobile,jProvinceNam,jCityName,jExpAreaName,jAddress,sname,smobile,
							sprovincenam,scityname,sexpareaname,saddress,GoodsName);
				JSONObject parseObject = JSON.parseObject(result);
				System.out.println(parseObject);
				JSONObject  Order=(JSONObject) parseObject.get("Order");
				System.out.println("Order");
				//将返回的运单号填入到用户的运单信息中
				String LogisticCode =Order.getString("LogisticCode");
				System.out.println("ssssssssssqwqeqeqe:"+LogisticCode);
				System.out.println(LogisticCode);
				 backOrder.setLogisticCode(LogisticCode);
				Boolean success = parseObject.getBoolean("Success");
				System.out.println(success);
				if(success.equals(true)) {
					backOrderService.insertBack_order(backOrder);
			    	backOrderService.insertAddress(backOrder);
			    	backOrderService.insertOrder(backOrder);
					return "index";
				}
					
			} catch (Exception e) {
				
				e.printStackTrace();
			}
			
    	 
			backOrderService.insertBack_order(backOrder);
	    	backOrderService.insertAddress(backOrder);
	    	backOrderService.insertOrder(backOrder);
    	
    	
    	return "index";
    }
    @RequestMapping("yuyuequjian2")
    public String submit2(HttpServletRequest request,HttpSession session,BackOrder backOrder){
		// 设置uid
		User user = (User) session.getAttribute("user");
		Integer uid = user.getUid();
		if (uid != null) {
			backOrder.setUid(uid);
		}
		// 设置订单生成时间
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = formatter.format(date);
		backOrder.setCreatetime(time);
		// 设置订单号码
		SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMddHHmmss");
		String time1 = formatter1.format(date);
		synchronized (time1) {
			backOrder.setOid(time1);
		}
		//设置金额
		 String payment= request.getParameter("pay_two");
		 backOrder.setPayment(payment);
		 //设置支付方式
		 backOrder.setPayment_type(1);
		 //设置订单状态
		 backOrder.setStatus(1);
		 //设置订单类型
		 backOrder.setOrder_type(1);
		 //设置运单编号
		 /*backOrder.setLogisticCode("123456789");*/
		 //生成自己联系快递订单
		 backOrderService.insertOrder(backOrder);
		 //设置回收订单中间表
		 //设置回收订单中间表back_id
		 Integer back_id=Integer.parseInt(request.getParameter("back_id_two"));
		 backOrder.setBack_id(back_id);
		 //设置回收数量
		 backOrder.setNum(1);
		 //设置回收评估值
		 String back_validss=request.getParameter("back_valid");
		 backOrder.setBack_assess(back_validss);
		 //设置回收方式
		 backOrder.setBack_way(2);
		 //生成自己联系快递中间表
		 backOrderService.insertBack_order(backOrder);
		 //删除原有地址
		 backOrderService.delectBackAddress(backOrder);
		 //获取上门的地址
		 String off=request.getParameter("off");
		 backOrder.setOff(off);
		 //获取上门详细地址
		 String address_detail=request.getParameter("address-detail-input");
		 backOrder.setAddress_detail(address_detail);
        //设置上门回收的地址表信息
		 backOrderService.insertBack_orders(backOrder);
		 
		 //生成订单后跳转到主页
    	return "index";
    }
    @RequestMapping("yuyuequjian4")
    public  String  submitOrder1(HttpSession session,BackOrder backOrder,HttpServletRequest request){
		// 设置uid
		User user = (User) session.getAttribute("user");
		Integer uid = user.getUid();
		if(uid!=null){
			backOrder.setUid(uid);
		}
		
		// 设置订单生成时间
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = formatter.format(date);
		backOrder.setCreatetime(time);
		// 设置订单号码
		SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMddHHmmss");
		String time1 = formatter1.format(date);
		synchronized (time1) {
			backOrder.setOid(time1);
		}
		//设置金额
		 String payment= request.getParameter("backprices");
		 backOrder.setPayment(payment);
		 //设置支付方式
		 backOrder.setPayment_type(1);
		 //设置订单状态
		 backOrder.setStatus(1);
		 //设置订单类型
		 backOrder.setOrder_type(1);
/*		 //设置运单编号
		 backOrder.setLogisticCode("123456789");*/
		 //生成自己联系快递订单
		 backOrderService.insertOrder(backOrder);
		 //设置回收订单中间表
		 //设置回收订单中间表back_id
		 Integer back_id=Integer.parseInt(request.getParameter("back_ids"));
		 backOrder.setBack_id(back_id);
		 //设置回收数量
		 backOrder.setNum(1);
		 //设置回收评估值
		 String back_validss=request.getParameter("back_validss");
		 backOrder.setBack_assess(back_validss);
		 //设置回收方式
		 backOrder.setBack_way(4);
		 //生成自己联系快递中间表
		 backOrderService.insertBack_order(backOrder);
		 //生成订单后跳转到主页
    	return "index";
    }
  @RequestMapping("yuyuequjian3")
  public String selectStore(HttpSession session,HttpServletRequest request,BackOrder backOrder){
	  User user = (User) session.getAttribute("user");
	    Integer uid=user.getUid();
	   	 Date date=new Date();
    	 SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    	 String time=formatter.format(date);
    	 SimpleDateFormat formatter1=new SimpleDateFormat("yyyyMMddHHmmss"); 
    	 String time1=formatter1.format(date);
    	 Long long1=date.getTime();
    	 String string= String.valueOf(long1);
    	 String store= request.getParameter("select");
    	 backOrder.setStore(store);
    	 synchronized (time1) {
    		 backOrder.setOid(time1);
		}
    	 
    	 backOrder.setPayment(request.getParameter("pay_one"));
    	 backOrder.setPayment_type(1);
    	 backOrder.setStatus(1);
          backOrder.setUid(uid);
    	 backOrder.setOrder_type(1);
    	 backOrder.setCreatetime(time);
    	 /*backOrder.setLogisticCode("123456789");*/
    	 backOrder.setBack_id(Integer.parseInt(request.getParameter("back_id_one")));
    	 backOrder.setNum(1);
    	 backOrder.setBack_assess(request.getParameter("back_valid_one"));
    	 backOrder.setBack_way(Integer.parseInt(request.getParameter("back_way_one")));
    	 backOrderService.delectBackStore(backOrder);
	  backOrderService.insertBack_store(backOrder);
	  backOrderService.insertOrder(backOrder);
	  backOrderService.insertBack_order(backOrder);
	  return "index";
  }
  @RequestMapping("checkPhone")
  @ResponseBody
  public boolean checkPhone(HttpServletRequest request,String phone){
	  boolean users=backOrderService.selectPhone(phone);
	  if(users==true){
		  return false;
	  }else{
		  return true;
	  }
	
	  
	  
	  
  }
  
  
}
